package com.softer.chengxin.project.controller;

import com.softer.chengxin.common.domin.Common;
import com.softer.chengxin.common.domin.CommonResult;
import com.softer.chengxin.common.domin.ManagerPageResult;
import com.softer.chengxin.common.domin.Page;
import com.softer.chengxin.common.enums.ResultEnum;
import com.softer.chengxin.project.domin.Project;
import com.softer.chengxin.project.service.ProjectService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Gong Zhiyang
 * @date 2018/9/27
 */
@RestController
@RequestMapping("/api/WY_ProjectBaseInfo")
public class ProjectController {
    @Autowired
    private ProjectService projectService;

    /**
     * 查询项目列表（用于下拉列表）
     *
     * @param session 缓存
     * @return 项目列表
     */
    @PostMapping("/queryProjectList")
    public List<Project> queryProjectList(HttpSession session) {
        //查询企业ID
        Integer id = (Integer) session.getAttribute(Common.ID);
        if (id == null) {
            return new ArrayList<>();
        }
        //获取项目ID
        String projectId = (String) session.getAttribute(Common.PROJECT_ID);
        if (StringUtils.isEmpty(projectId)) {
            projectId = Common.ZERO;
        }
        return projectService.queryProjectList(id, projectId);
    }

    /**
     * 项目下拉列表，用于人员调动
     *
     * @param session 缓存
     * @return 项目列表
     */
    @PostMapping("/getProjectListForPerson")
    public List<Project> getProjectListForPerson(HttpSession session) {
        //查询企业ID
        Integer id = (Integer) session.getAttribute(Common.ID);
        if (id == null) {
            return new ArrayList<>();
        }
        //获取项目ID
        String projectId = (String) session.getAttribute(Common.PROJECT_ID);
        if (StringUtils.isEmpty(projectId)) {
            projectId = Common.ZERO;
        }
        return projectService.getProjectListForPerson(id, projectId);
    }

    /**
     * 查询企业下所有项目
     *
     * @param session 缓存
     * @return 项目列表
     */
    @PostMapping("/getAllProject")
    public List<Project> getAllProject(HttpSession session) {
        //查询企业ID
        Integer id = (Integer) session.getAttribute(Common.ID);
        if (id == null) {
            return new ArrayList<>();
        }
        return projectService.getAllProject(id);
    }

    /**
     * 删除项目
     *
     * @param id 项目ID
     * @return 结果
     */
    @PostMapping("/delete")
    public CommonResult delete(Integer id) {
        return projectService.delete(id);
    }

    /**
     * 提交项目
     *
     * @param id 项目ID
     * @return 结果
     */
    @PostMapping("/submit")
    public CommonResult submit(Integer id) {
        return projectService.submit(id);
    }

    /**
     * 查询项目信息列表
     *
     * @param page    页面实体
     * @param project 查询实体
     * @return 列表
     */
    @PostMapping("/query")
    public ManagerPageResult query(Page page, Project project, HttpSession session) {
        //查询企业ID
        Integer id = (Integer) session.getAttribute(Common.ID);
        if (id == null) {
            return new ManagerPageResult();
        }
        project.setCompanyId(id);
        //获取项目ID
        String projectId = (String) session.getAttribute(Common.PROJECT_ID);
        if (StringUtils.isBlank(projectId)) {
            projectId = Common.ZERO;
        }
        project.setProjectId(projectId);
        return projectService.query(page, project);
    }

    /**
     * 增加项目信息
     *
     * @param project 项目实体
     * @return 增加结果
     */
    @PostMapping("/add")
    public CommonResult add(Project project, HttpSession session) {
        //查询企业ID
        Integer id = (Integer) session.getAttribute(Common.ID);
        if (id == null) {
            return new CommonResult(ResultEnum.FAIL.getCode(), "请登陆后操作");
        }
        project.setCompanyId(id);
        checkDate(project);
        return projectService.add(project);

    }

    private void checkDate(Project project) {
        try {
            if (StringUtils.isBlank(project.getCommitteeTime())) {
                project.setCommitteeTime(null);
            }
            if (StringUtils.isBlank(project.getFinishTime())) {
                project.setFinishTime(null);
            }
            if (StringUtils.isBlank(project.getTakeoverTime())) {
                project.setTakeoverTime(null);
            }
            if (StringUtils.isBlank(project.getEntrustBeginTime())) {
                project.setEntrustBeginTime(null);
            }
            if (StringUtils.isBlank(project.getEntrustEndTime())) {
                project.setEntrustEndTime(null);
            }
            if (StringUtils.isBlank(project.getFoundingTime())) {
                project.setFoundingTime(null);
            }
            if (StringUtils.isBlank(project.getTerm())) {
                project.setTerm(null);
            }

        } catch (Exception e) {
            e.getStackTrace();
        }

    }

    /**
     * 查询具体项目信息
     *
     * @param id 项目ID
     * @return 项目信息
     */
    @PostMapping("/detail")
    public Project detail(Integer id) {
        return projectService.detail(id);
    }

    /**
     * 编辑项目信息
     *
     * @param project 项目实体
     * @return 编辑结果
     */
    @PostMapping("/edit")
    public CommonResult edit(Project project) {
        checkDate(project);
        return projectService.edit(project);
    }
    @PostMapping("/selectAllProject")
    public List<Project> add(HttpSession session) {
        //  登录名
        // 查询企业ID
        Integer id = (Integer) session.getAttribute(Common.ID);
        return projectService.selectAllProject(id);
    }
}
